package com.yxw.springmvc.userBehavior.util;

import java.security.MessageDigest;

/**
 * Created by hp on 2016/9/21.
 */
public class DigestUtils {
    private DigestUtils(){}

    public static String encodeMD5(String text, boolean isCapital) {
        return encode(text, Algorithm.MD5, isCapital);
    }

    public static String encodeMD5(String text) {
        return encodeMD5(text, true);
    }

    public static String encodeSHA(String text) {
        return encodeSHA(text, true);
    }

    public static String encodeSHA(String text, boolean isCapital) {
        return encode(text, Algorithm.SHA, isCapital);
    }

    public static String encode(String text, Algorithm algorithm, boolean isCapital) {
        try {
            return encode(text.getBytes("UTF-8"), algorithm, isCapital);
        } catch (Throwable throwable) {
            throw new RuntimeException(throwable);
        }
    }

    public static String encode(byte[] data, Algorithm algorithm, boolean isCapital) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithm.name());
            byte[] ab = md.digest(data);
            return HexUtils.byteArrayToHex(ab, isCapital);
        } catch (Throwable throwable) {
            throw new RuntimeException(throwable);
        }
    }

    public static enum Algorithm {
        MD5,
        SHA,;
    }
}
